package p.lodz.pl.poid.zad2.controller;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.MenuBar;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.FileChooser;
import javax.imageio.ImageIO;
import p.lodz.pl.poid.zad2.view.FFTPanel;
import p.lodz.pl.poid.zad2.view.FiltersPanel;
import p.lodz.pl.poid.zad2.view.PhasePanel;
import p.lodz.pl.poid.zad2.view.SegmentationPanel;

/**
 *
 * @author janiu
 */
public class MainWindowController extends AnchorPane implements Initializable, Controllable {

    @FXML
    private ImageView sourceImage;

    @FXML
    private ImageView resultImage;
    @FXML
    private StackPane stackPane;
    
    private BufferedImage bufferedImage;
    private Scene scene;
    private Image image;
        

    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }
    
    @FXML
    private void handleOpenImage(final ActionEvent event) {
        sourceImage.getImage();
        selectImage();
    }
    
    @FXML
    private void handleFastFourierTransform(final ActionEvent event) {
        FFTPanel panel = new FFTPanel();
        panel.setImage(image);
        panel.setResultImage(resultImage);
        panel.setBufferedImage(bufferedImage);
        stackPane.getChildren().clear();
        stackPane.getChildren().addAll(panel);
    }
    
    @FXML
    private void handleFilters(final ActionEvent event) {
        FiltersPanel panel = new FiltersPanel();
        panel.setImage(image);
        panel.setResultImage(resultImage);
        panel.setBufferedImage(bufferedImage);
        stackPane.getChildren().clear();
        stackPane.getChildren().addAll(panel);        
    }
    
    @FXML
    private void handlePhase(final ActionEvent event) {
        PhasePanel panel = new PhasePanel();
        panel.setImage(image);
        panel.setResultImage(resultImage);
        panel.setBufferedImage(bufferedImage);
        stackPane.getChildren().clear();
        stackPane.getChildren().addAll(panel);          
    }
    
    @FXML
    private void handleSegmentation(final ActionEvent event) {
        SegmentationPanel panel = new SegmentationPanel();
        panel.setImage(image);
        panel.setResultImage(resultImage);
        panel.setBufferedImage(bufferedImage);
        stackPane.getChildren().clear();
        stackPane.getChildren().addAll(panel);          
    }

    private void selectImage() {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Open Image");
        File file = fileChooser.showOpenDialog(scene.getWindow());
        if (file != null) {
            setImage(file);
        }
    }

    private void setImage(File file) {
        image = new Image(file.toURI().toString());//, 600, 600, false, false);
        readBufferedImage(file);
        sourceImage.setImage(image);
    }

    private void readBufferedImage(File file) {
        try {
            bufferedImage = ImageIO.read(file);
        } catch (IOException ex) {
            Logger.getLogger(MenuBar.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public BufferedImage getBufferedImage() {
        return bufferedImage;
    }

    @Override
    public void setScene(Scene scene) {
        this.scene = scene;
    }

}
